package gw.com.android.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppMain;
import www.com.library.dialog.ToastPopWindow;
import www.com.library.model.DataItemDetail;
import www.com.library.util.DeviceUtil;

/**
 * Created by jay on 2017/7/4.
 */

public class BindEnterPasswordDialog extends BaseDialog{

    private DataItemDetail mResult;
    private String mContent = "";
    private TextView mView;
    private TextView mErrorView;
    private EditText mPassWordView;
    private String mPassWord;
    BtnConfirmListener callback;
    private static boolean isPhoneLogin;
    private static BindEnterPasswordDialog mInstance = null;

    public static BindEnterPasswordDialog  instance(){
        return mInstance;
    }
    public BindEnterPasswordDialog(Context context, String content, DataItemDetail mResult, BtnConfirmListener callback) {
        super(context);
        this.mResult = mResult;
        this.callback = callback;
        mContent = content;
    }

    /**
     * 显示弹层
     * @param act
     */
    public static void showBindEnterPasswordDialog(Activity act, boolean isPhone, String content, DataItemDetail mResult, BtnConfirmListener callback){
        isPhoneLogin = isPhone;
        mInstance = new BindEnterPasswordDialog(act,content,mResult,callback);
        mInstance.setCanceledOnTouchOutside(false);
        mInstance.show();
    }

    @Override
    public void initParam() {
        mCustomViewResId = R.layout.dialog_bind_enter_password_layout;
        if(isPhoneLogin){
            mBtnPosText = AppMain.getAppString(R.string.btn_confirm);
            mBtnNegText = AppMain.getAppString(R.string.phone_bind_cancel);
        }else{
            mBtnPosText = AppMain.getAppString(R.string.btn_confirm);
            mBtnNegText = AppMain.getAppString(R.string.btn_cancel);
        }
        int padding = (int) (15 * DeviceUtil.instance().getScreenDensity(mOwnerAct));
        setPadding(padding, 0, padding, 0);
    }

    @Override
    public void inflaterCustomView(View mCustomView) {
        mView = (TextView) mCustomView.findViewById(R.id.dialog_title_text);
        mPassWordView =  (EditText) mCustomView.findViewById(R.id.password_et);
        mErrorView = (TextView) mCustomView.findViewById(R.id.error_msg);
        if(null != mBtnNeg && isPhoneLogin){
            mBtnNeg.setTextSize(14);
        }
        if(mContent.length() > 0){
            mView.setText(mContent);
        }
    }
    @Override
    public void onClick(View v) {
//        dismiss();
        switch (v.getId()) {
            // 确认绑定
            case R.id.action_btn_pos:
//                callback.onBtnClick(checkedItem);
                mPassWord = mPassWordView.getText().toString().trim();
                if (TextUtils.isEmpty(mPassWord)){
                    new ToastPopWindow(mOwnerAct, AppMain.getAppString(R.string.login_password_null)).show();
                    return;
                }
                setmErrorViewVisble(false);
                callback.onBtnClick(mResult,mPassWord);
//                dismiss();

                break;
            // 取消
            case R.id.action_btn_neg:
                dismiss();
                break;
        }
    }

    public void setmErrorViewVisble(boolean isVisble){
        if(isVisble){
            mErrorView.setVisibility(View.VISIBLE);
        }else{
            mErrorView.setVisibility(View.GONE);
        }

    }

    public interface BtnConfirmListener {
        /**
         * 按钮点击监听器回调函数
         *
         * @param item 选中的账户信息,输入的密码
         */
        void onBtnClick(DataItemDetail item,String mPassWord);
    }
}
